import axios from '@/libs/axios1.js'
import { escape2Html, noFilter } from '@/libs/utils'
import { param } from 'jquery'
let jsonHeaders = {'content-type': 'application/json;charset=UTF-8'}

// 创建资源关联的脚本
export const bindScriptResource = params =>{
  return axios({
    url:'/new-paper/script/associate',
    method:'POST',
    headers:jsonHeaders,
    params
  })
}

// 清空资源关联的脚本
export const deleteScriptResource = params =>{
  return axios({
    url:'/new-paper/script/disassociate',
    method:'delete',
    headers:jsonHeaders,
    params
  })
}

// 查询资源关联的脚本
export const getScriptResource = ({type, id}) =>{
  return axios({
    url:`/new-paper/script/relation/${type}/${id}`,
    method:'get',
    headers:jsonHeaders
  })
}

//创建ctf试题
export const createCtf = params =>{
  return axios({
    url:'/new-paper/subjective/question',
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改ctf试题
export const modifyCtf = (params,id) =>{
  return axios({
    url:`/new-paper/subjective/question/${id}`,
    method:'PUT',
    headers:jsonHeaders,
    data:params
  })
}

//删除Ctf
export const delCtf = id =>{
  return axios({
    url:`/new-paper/subjective/question/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}


//查询Ctf试题列表
export const queryCtfList = (params, query, queryAll) => {
  let copy = Object.assign({}, params) // 拷贝一份，不然有影响到页面数据
  if (copy.needEnv == "true") {
    copy.needEnv=1
  } else if (copy.needEnv == "false") {
    copy.needEnv=0
  }
  if (copy.id === undefined) { // 旧接口的id参数携带在chaId中，id没有设置时尝试获取旧参数
    copy.id = copy.chaId
  }
  if (copy.needEnv === undefined) { // needEnv没有正确设置，尝试从isNeedEnv获取
    copy.needEnv = Number(copy.isNeedEnv)
  }
  if (query === undefined) { // 原页面调用，分页数据保存在params中，没有传递query，需要重新设值
    query = {
      pageIndex: copy.pageIndex,
      pageSize: copy.pageSize
    }
  }
  /**
   * 向前兼容，queryAll、matchMode都没有传参时，需要将matchMode设置为1,2，只查询夺旗/渗透、攻防/夺旗/渗透两种类型
   * 否则需要连带问答题一起查询
   */
  // if (!queryAll && !copy.matchMode) {
  //   copy.matchMode = '1,2'
  // }
  return new Promise(resolve => {
    axios({
      url:'/new-paper/subjective/question/list',
      method: 'POST',
      data:copy,
      params:query,
      headers:jsonHeaders
    }).then(res => {
      if (res && res.data && res.data.items) {
        res.data.items = res.data.items.map(item => {
          item.category = escape2Html(item.category)
          if (item.chaId === undefined) {
            item.chaId = item.id
          }
          return item
        })
      }
      resolve(res)
    })
  })
}

//添加ctf试题标签
export const createCtfTags = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/createCtfTags`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改ctf试题标签
export const modifyCtfTags = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/modifyCtfTags`,
    method:'PUT',
    headers:jsonHeaders,
    data:params
  })
}

//删除ctf试题标签
export const delCtfTags = id =>{
  return axios({
    url:`/exampaper/examPaperLibs/removeCtfTags/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}

//查询ctf试题标签
export const queryCtfTags = chaId =>{
  return axios({
    url:`/exampaper/examPaperLibs/ctfTags/${chaId}`,
    method:'GET',
    headers:jsonHeaders
  })
}

//添加ctf试题url
export const createCtfUrls = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/createCtfUrls`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改ctf试题url
export const modifyCtfUrls = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/modifyCtfUrls`,
    method:'PUT',
    headers:jsonHeaders,
    data:params
  })
}

//删除ctf试题url
export const delCtfUrls = id =>{
  return axios({
    url:`/exampaper/examPaperLibs/removeCtfUrls/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}

//查询ctf试题url
export const queryCtfUrls = chaId =>{
  return axios({
    url:`/exampaper/examPaperLibs/ctfUrls/${chaId}`,
    method:'GET',
    headers:jsonHeaders
  })
}
//添加ctf试题答案
export const createCtfKeys = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/createCtfKeys`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改ctf试题答案
export const modifyCtfKeys = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/modifyCtfKeys`,
    method:'PUT',
    headers:jsonHeaders,
    data:params
  })
}

//删除ctf试题答案
export const delCtfKeys = id =>{
  return axios({
    url:`/exampaper/examPaperLibs/removeCtfKeys/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}

//查询ctf试题答案
export const queryCtfKeys = chaId =>{
  return axios({
    url:`/exampaper/examPaperLibs/ctfKeys/${chaId}`,
    method:'GET',
    headers:jsonHeaders
  })
}

//创建ctf试题file
export const createCtfFiles = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/createCtfFiles`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//删除ctf试题file
export const delCtfFiles = id =>{
  return axios({
    url:`/exampaper/examPaperLibs/removeCtfFiles/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}

export const queryCtfFiles = chaId => {
	return axios({
    url:`/exampaper/examPaperLibs/ctfFiles/${chaId}`,
    method:'GET',
    headers:jsonHeaders
  })
}

//删除提交文件
export const delfile = params => {
  return axios({
    url: `/minio/upload/file/${params}`,
    method: 'delete',
    headers: jsonHeaders
  })
}

//创建客观试题
export const createQuestion = params =>{
  return axios({
    // url:`/exampaper/examPaperLibs/createQuestion`,
    url:`new-paper/objective/question`,
    method:'POST',
    headers:jsonHeaders,
    data:params
  })
}

//修改客观试题
export const modifyQuestion = (params,id) =>{
  return axios({
    // url:`/exampaper/examPaperLibs/modifyQuestion`,
    url:`/new-paper/objective/question/${id}`,
    method:'PUT',
    headers:jsonHeaders,
    data:params
  })
}

//删除客观
export const delQuestion = id =>{
  return axios({
    // url:`/exampaper/examPaperLibs/removeQuestion/${id}`,
    url:`/new-paper/objective/question/${id}`,
    method:'DELETE',
    headers:jsonHeaders
  })
}

//查询客观试题列表
export const queryObjectiveList = (params,query) =>{
  return new Promise(resolve => {
    axios({
      // url:`/exampaper/examPaperLibs/objectivePages`,
      url:`/new-paper/objective/question/list`,
      method: 'POST',
      data:params,
      params:query,
      headers: jsonHeaders,
      timeout: 3600 * 1000, // 限制请求超时时间3600 * 1000 = 1 hour  指定请求超时的毫秒数(0 表示无超时时间)
    }).then(res => {
      res.data && res.data.items && res.data.items.forEach(item => {
        if ([1, 2].includes(item.modeCode)) {
          item.answers && item.answers.forEach((subItem, index) => {
            if(subItem.description.indexOf('】') == 2 || subItem.description.indexOf(']') == 2) {
              subItem.description = subItem.description.slice(3);
            }
            subItem.description = `[${noFilter(index)}]${subItem.description}`
          })
        }
      })
      resolve(res)
    })
  });
}

//查询客观试题列表（新接口只查单选与填空两类）
export const queryObjectiveListByQuiz = params => {
  return axios({
    url:`/exampaper/examPaperLibs/objectivePages/quiz`,
    method:'GET',
    params:params,
    headers:jsonHeaders
  })
}

//查询客观试题列表(排除课件中使用的题目)
export const queryObjectiveListNoGEQuesPage = params =>{
  return new Promise(resolve => {
    axios({
      // url:`/exampaper/examPaperLibs/noGEQuesPage`,
      url:`/new-paper/objective/question/noGuideExpQues`,
      method:'GET',
      params:params,
      headers:jsonHeaders,
      timeout:240000
    }).then(res => {
      res.data && res.data.items && res.data.items.forEach(item => {
        if ([1, 2].includes(item.modeCode)) {
          item.answers && item.answers.forEach((subItem, index) => {
            if(subItem.description.indexOf('】') == 2 || subItem.description.indexOf(']') == 2) {
              subItem.description = subItem.description.slice(3);
            }
            subItem.description = `[${noFilter(index)}]${subItem.description}`
          })
        }
      })
      resolve(res)
    })
  });
}

//查询客观题答案
export const queryObjectiveAnswerList = quesionId =>{
  return new Promise(resolve => {
    axios({
      url:`/new-paper/objective/question/detail/${quesionId}`,
      method:'GET',
      headers:jsonHeaders
    }).then(res => {
      if (res.data) {
        if ([1, 2].includes(res.data.modeCode)) {
          res.data.answers && res.data.answers.forEach((subItem, index) => {
            if(subItem.description.indexOf('】') == 2 || subItem.description.indexOf(']') == 2) {
              subItem.description = subItem.description.slice(3);
            }
            subItem.description = `[${noFilter(index)}]${subItem.description}`
          })
        }
      }
      resolve(res)
    })
  });
}
//查询实操题列表
export const queryOprateList = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/practicePages`,
    method:'GET',
    params:params,
    headers:jsonHeaders
  })
}

//创建实操题
export const addOprateQuestion = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/createPractice`,
    method:'post',
    data:params,
    headers:jsonHeaders
  })
}
//修改实操题
export const modifyOprateQuestion = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/modifyPractice`,
    method:'put',
    data:params,
    headers:jsonHeaders
  })
}
//删除实操题
export const delOprateQuestion = params =>{
  return axios({
    url:`/exampaper/examPaperLibs/removePractice/${params}`,
    method:'delete',
    headers:jsonHeaders
  })
}

//根据题目id获取题目答案
export const getQuestionAndAnswer = (id) => {
  return new Promise(resolve => {
    axios({
      url:`/new-paper/objective/question/detail/${id}`,
      method: 'get',
      headers: jsonHeaders
    }).then(res => {
      if (res.data) {
        if ([1, 2].includes(res.data.modeCode)) {
          res.data.answers && res.data.answers.forEach((subItem, index) => {
            if(subItem.description.indexOf('】') == 2 || subItem.description.indexOf(']') == 2) {
              subItem.description = subItem.description.slice(3);
            }
            subItem.description = `[${noFilter(index)}]${subItem.description}`
          })
        }
      }
      resolve(res)
    })
  });
}

//获取所有镜像
export const getallImage = (obj,search)=>{
  return axios({
    // url:`/virtual/image/list`,
    url:`/resource-manager/image/list`,
    data: obj,
    params:search,
    method:'post',
    headers:jsonHeaders
  })
}

//获取所有配额
export const getallFlavor = (search) => {
  return axios({
    url: `/resource-manager/flavor/list`,
    params:search,
    method: 'get',
    headers: jsonHeaders
  })
}

//导出客观题
export const exportQuestions = params =>{
  return axios({
    url:`/new-paper/objective/question/export`,
    method:'post',
    data:params,
    headers:{
        'Content-Type': 'application/json'
    },
    responseType: 'blob',
    timeout:0
  })
}

//获取赛题详情
export const getQuestionDetail = (id) => {
  return axios({
    url: `/new-paper/subjective/question/detail/${id}`,
    method: 'get',
    headers: jsonHeaders
  })
}